package so.shanku.essential.activity.base;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;
import aym.util.getservicesdata.GetServicesDataUtil;
import aym.view.toast.ToastUtil;
import cn.jpush.android.api.JPushInterface;
import net.tsz.afinal.AFinalActivity;
import so.shanku.essential.R;
import so.shanku.essential.activity.*;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.LogUtil;
import so.shanku.essential.utils.Utils;
import so.shanku.essential.view.WihteRoundCornersDialog;
import striveen.util.used.view.view.MyLoadingDataDialogManager;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/8/17 0017.
 * 最基础的类 提供基本的公共方法
 */
public class BaseActivity extends AFinalActivity {
    /**
     * 提示toast的展示对象 可直接调用期show方法展示文字
     */
    public ToastUtil toast;

    /**
     * 模拟toast展示加载数据的对话框，使界面更加友好
     */
    public MyLoadingDataDialogManager loadingToast;
    protected MyApplication myApplication;

    /**
     * 调用服务器接口获取服务器数据的工具
     */
    public GetServicesDataUtil getDataUtil;
    protected BaseActivity mContext;

    /**
     * 初始化了toast 同时也初始化加载数据对话框
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        toast = ToastUtil.initToast(this);
        loadingToast = MyLoadingDataDialogManager.init(this);
        myApplication = MyApplication.getInstance();
        getDataUtil = GetServicesDataUtil.init();
        mContext = this;
        LogUtil.d(TAG,"onCreate");
    }

    /**
     * 将展示的加载数据的对话框释放掉
     */
    @Override
    protected void onDestroy() {
        LogUtil.d(TAG,"onDestroy");
        if (null != loadingToast) {
            loadingToast.destroy();
        }
        super.onDestroy();
    }

    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }


    /**
     * 清空历史记录
     *
     * @param clzz
     */
    public void jumpToAndClearAll(Class clzz) {
        Intent intent = new Intent(this, clzz);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

    }

    public void jumpTo(Intent intent, boolean isFinish) {
        startActivity(intent);
        if (isFinish) {
            this.finish();
        }
    }

    /**
     * activity 跳转
     *
     * @param clzz 跳转到的activity
     */
    public void jumpTo(Class clzz) {
        jumpTo(clzz, false);
    }

    public void jumpTo(Class clzz, String actionName) {
        jumpTo(clzz, actionName, false);
    }

    public void jumpTo(Class clzz, boolean isFinish) {
        jumpTo(clzz, "", isFinish);
    }

    /**
     * activity 跳转
     *
     * @param clzz     跳转到的activity
     * @param bundle   携带的数据
     * @param isFinish 是否销毁当前的activity
     */
    public void jumpTo(Class clzz, Bundle bundle, boolean isFinish) {
        jumpTo(clzz, null, bundle, isFinish);
    }


    public void jumpTo(Class clzz, String actionName, boolean isFinish) {
        jumpTo(clzz, actionName, null, isFinish);
    }

    /**
     * @param clzz
     * @param actionName
     * @param bundle
     * @param isFinish
     */
    public void jumpTo(Class clzz, String actionName, Bundle bundle, boolean isFinish) {
        Intent startIntent = new Intent(this, clzz);
        if (!TextUtils.isEmpty(actionName)) {
            startIntent.setAction(actionName);
        }
        if (bundle != null) {
            startIntent.putExtra(Constant.BUNDLE_EXTRA, bundle);
        }
        startActivity(startIntent);
        if (isFinish) {
            this.finish();
        }
    }


    protected void addUnderLine(TextView vistor_login_tv) {
        Utils.addUnderLine(vistor_login_tv);
    }

    /**
     * 添加关注
     */
    public void getDate_GetFocusOn(GetServicesDataUtil.IGetServicesDataCallBack callBack, String vendorId, Context context) {
        if (Utils.isBrandFastClick()) {
            return;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);
        params.put("VendorId", vendorId);
        params.put("UserInfoId", Utils.getUserId(context));
        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.GetAction_GetFocusOn);
        queue.setCallBack(callBack);
        queue.setWhat(GetDataConfing.WHAT_GET_FOCUS_ON);
        queue.setParamsNoJson(params);
        getDataUtil.getData(queue);
    }

    /**
     * 收藏商品
     *
     * @param goodsId
     * @param callBack
     */
    public void changeGoodsCollect(String goodsId, GetServicesDataUtil.IGetServicesDataCallBack callBack) {
        if (Utils.isFastClick()) {
            return;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(GetDataQueue.Params_key,
                GetDataConfing.Key_productAddUserAttention);
        params.put("userName", Utils.getUserAccount(this));
        params.put("proId", goodsId);

        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_productAddUserAttention);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(GetDataConfing.What_productAddUserAttention);
        getDataUtil.getData(queue);
    }

    /**
     * 更新本地关注的店铺
     *
     * @param vendorId
     * @param b
     */
    public void updateAttentionStatues(String vendorId, boolean b) {
        if (b) {
            MyApplication.getInstance().addAttendBrands(vendorId);
        } else {
            MyApplication.getInstance().removeAttendBrands(vendorId);
        }
    }

    /**
     * 进入商品详情
     *
     * @param goodsId
     */
    public void seeProductDetail(String goodsId) {
        Intent intent=new Intent(this,ProductDetailActivity.class);
        intent.putExtra(Constant.ID, goodsId);
        jumpTo(intent, false);
    }

    /**
     * 查看主题
     *
     * @param vendorId
     */
    public void seeShopThemeDetail(String vendorId) {
        Intent intent=new Intent(this,ShopThemeActivity.class);
        intent.putExtra(Constant.ID,vendorId);
        jumpTo(intent, false);
    }

    /**
     * 拨打电话
     *
     * @param phone
     */
    public void startTelPhone(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        jumpTo(intent, false);
    }

    /**
     * 查看物流
     */
    protected void seeLogistics(String orderNum) {
        Intent intentSeeLogistics = new Intent(this, UrlWebviewActivity.class);
        intentSeeLogistics.putExtra(ExtraKeys.Key_Msg1, 2);
        intentSeeLogistics.putExtra(ExtraKeys.Key_Msg2, orderNum);
        jumpTo(intentSeeLogistics,false);
    }

    /**
     * 查看退款详情
     * @param orderNum
     */
    protected void scanRefundDetail(String orderNum) {
        Intent intentSeeLogistics = new Intent(this, UrlWebviewActivity.class);
        intentSeeLogistics.putExtra(ExtraKeys.Key_Msg1, 3);
        intentSeeLogistics.putExtra(ExtraKeys.Key_Msg2, orderNum);
        jumpTo(intentSeeLogistics,false);
    }

    /**
     * 查看订单详情
     *
     * @param orderId
     * @param orderNum
     * @param vendorId
     */
    public void seeOrderDetail(String orderNum) {
        Intent intentSeeLogistics = new Intent(this, UserOrderDetailActivity.class);
        intentSeeLogistics.putExtra(ExtraKeys.Key_Msg1, orderNum);
        jumpTo(intentSeeLogistics, false);
    }

    /**
     * 确认收货
     *
     * @param orderNum
     * @param callBack
     */
    protected void goodsReceiveConfirm(String orderNum, GetServicesDataUtil.IGetServicesDataCallBack callBack) {
        showConfirmDialog(orderNum, callBack, GetDataConfing.What_ConfirmOrdersByOrderNum);
    }

    /**
     * 取消订单接口
     */
    public void getData_Cancel_Order(String orderId, GetServicesDataUtil.IGetServicesDataCallBack callBack) {
        // loadingToast.show();
//        Map<String, Object> params = new HashMap<String, Object>();
//        params.put(GetDataQueue.Params_key, "OrderNum");
//        params.put("orderNum", orderNum);
//        GetDataQueue queue = new GetDataQueue();
//        queue.setCallBack(callBack);
//        queue.setActionName(GetDataConfing.GetAction_UpdateOrdersbyOrderNum);
//        queue.setWhat(GetDataConfing.what_UpdateOrdersbyOrderNum);
//        queue.setParamsNoJson(params);
//        getDataUtil.getData(queue);
        showConfirmDialog(orderId, callBack, GetDataConfing.what_UpdateOrdersbyOrderNum);
    }

    /**
     * 退货退款
     *
     * @param orderNum
     * @param callBack
     */
    protected void getFundReturn(String orderNum, GetServicesDataUtil.IGetServicesDataCallBack callBack) {
         showConfirmDialog(orderNum, callBack, GetDataConfing.What_ReturnFundByOrderNum);
        //doGoodsReturn(orderNum,"183");
    }


    /**
     * s申请退货
     *
     * @param orderNum
     * @param callBack
     */
    protected void getGoodsReturn(String orderNum, GetServicesDataUtil.IGetServicesDataCallBack callBack) {
        //getFundReturn(orderNum,callBack);
        Intent intent = new Intent(this, ApplyReturnProductActivity.class);
        intent.putExtra(ExtraKeys.Key_Msg1, orderNum);
        jumpTo(intent, false);
    }

    /**
     * 进行退款 填写退款理由
     *
     * @param orderNum  退货单号
     */
    public void doGoodsReturn(String orderNum,String vendorId) {
        Intent intent = new Intent(this, DoReturnProductActivity.class);
        intent.putExtra(ExtraKeys.Key_Msg1, orderNum);
        intent.putExtra(ExtraKeys.Key_Msg2,vendorId);
        jumpTo(intent, false);
    }

    /**
     * 查看退货进度
     * @param orderNum    退货单号
     */
    public void scanReturnGoodsProgress(String orderNum) {
        Intent intentSeeLogistics = new Intent(this, UrlWebviewActivity.class);
        intentSeeLogistics.putExtra(ExtraKeys.Key_Msg1, 4);
        intentSeeLogistics.putExtra(ExtraKeys.Key_Msg2, orderNum);
        this.startActivity(intentSeeLogistics);
    }

    /**
     * 查看退货详情
     * @param orderNum  退货单号
     */
    public void scanReturnGoodsDetail(String orderNum) {
        Intent intentSeeLogistics = new Intent(this, UrlWebviewActivity.class);
        intentSeeLogistics.putExtra(ExtraKeys.Key_Msg1, 5);
        intentSeeLogistics.putExtra(ExtraKeys.Key_Msg2, orderNum);
        this.startActivity(intentSeeLogistics);
    }

    public void gotoLogin() {
        Intent intent = new Intent(this, NoLoginActivity.class);
        intent.putExtra(ExtraKeys.Key_Msg1, 1);
        jumpTo(intent, false);
    }

    WihteRoundCornersDialog builder;

    /**
     * 弹框确认
     * @param orderNum  订单编号或者订单的key id
     * @param callBack
     * @param type
     */
    private void showConfirmDialog(final String orderNum, final GetServicesDataUtil.IGetServicesDataCallBack callBack, final int type) {

        builder = new WihteRoundCornersDialog(this, R.style.ExitDialogStyle, 2, new WihteRoundCornersDialog.DialogCallBack() {
            @Override
            public void bttonclick(int index) {
                builder.dismiss();
                switch (index) {
                    case 1:
                        break;
                    case 2:
                        loadingToast.show();
                        Map<String, Object> params = new HashMap<String, Object>();
                        params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);
                        GetDataQueue queue = new GetDataQueue();
                        queue.setCallBack(callBack);
                        queue.setWhat(type);

                        if (type == GetDataConfing.What_ConfirmOrdersByOrderNum) { //确认收货
                            params.put("orderNum", orderNum);

                            queue.setActionName(GetDataConfing.Action_ConfirmOrdersByOrderNum);
                        } else if (type == GetDataConfing.what_UpdateOrdersbyOrderNum) { //取消订单
                            params.put("OrderId", orderNum);
                            params.put("UserId", Utils.getUserId(mContext));

                            queue.setActionName(GetDataConfing.GetAction_UpdateOrdersbyOrderNum);
                        } else if (type == GetDataConfing.What_ReturnFundByOrderNum) {
                            params.put("OrderId", orderNum);
                            params.put("UserId", Utils.getUserId(mContext));

                            queue.setActionName(GetDataConfing.Action_ReturnFundByOrderNum);
                        }
                        queue.setParamsNoJson(params);
                        getDataUtil.getData(queue);
                        break;
                }
            }

            @Override
            public void bttonclick(int index, int what) {

            }
        });
        builder.setTitletext(R.string.shopping_prompt);
        if (type == GetDataConfing.What_ConfirmOrdersByOrderNum) {
            builder.setMessagetext(R.string.goods_receiver_confirm_msg);

        } else if (type == GetDataConfing.what_UpdateOrdersbyOrderNum) {
            builder.setMessagetext(R.string.order_cancel_confirm_msg);
        } else if (type == GetDataConfing.What_ReturnFundByOrderNum) {
            builder.setMessagetext(R.string.apply_return_fund_confirm_msg);
        }
        builder.setButtonText(R.string.cancel, R.string.ensure);
        builder.show();
    }


}